Repository navigation
Support chained SeekHead elements in MatroskaExtractor - #3597
Tripplesixty wants to merge 1 commit into
Conversation
| // The secondary SeekHead pointer landed on a non-SeekHead element; stop retrying and | ||
| // proceed with tracks/seek-map setup so the extractor returns to the Cluster. | ||
| finishSecondarySeekHead(); | ||
| } |
There was a problem hiding this comment.
When id != ID_SEEK_HEAD, control still falls through into switch (id) for the unexpected element, so a bogus pointer landing on ID_SEGMENT, ID_TRACK_ENTRY, or ID_CHAPTER_ATOM can throw or mutate parser state before seeking back. Returning early after finishSecondarySeekHead() avoids running switch (id) on that element, though DefaultEbmlReader will still have pushed that master element onto its stack until its end position is passed.
There was a problem hiding this comment.
In startMasterElement / endMasterElement I added an early return after finishSecondarySeekHead() when id != ID_SEEK_HEAD, and tracked ignoredSecondarySeekHeadElementId so endMasterElement skips the matching pop from DefaultEbmlReader's stack (which otherwise caused ID_TRACK_ENTRY to hit checkNotNull(this.currentTrack)).
| continueReading = reader.read(input); | ||
| if (continueReading | ||
| && (maybeSeekForTracks(seekPosition, input.getPosition()) | ||
| && (maybeSeekForSeekHead(seekPosition, input.getPosition()) |
There was a problem hiding this comment.
If secondarySeekHeadPosition lands on a non-master element near EOF (such as trailing Void padding) or a truncated secondary SeekHead, reader.read(input) returns false before startMasterElement or endMasterElement(ID_SEEK_HEAD) can run. The loop then exits to RESULT_END_OF_INPUT with seekPositionAfterReadingSeekHead still set, never calling finishSecondarySeekHead() or seeking back to the first cluster (and leaving deferred track formats unsent). Recovering in the !continueReading branch when seekPositionAfterReadingSeekHead != C.INDEX_UNSET would let playback still proceed from the cluster.
There was a problem hiding this comment.
I added a recovery in read()'s !continueReading branch when seekPositionAfterReadingSeekHead != C.INDEX_UNSET to call finishSecondarySeekHead() and return Extractor.RESULT_SEEK back to the first Cluster.
RFC 9559 Section 6.3 states that a Matroska Segment may contain a primary SeekHead near the beginning of the file that references a single secondary SeekHead (typically written near the end of the file by muxers such as mkvmerge) containing the positions of Tracks and/or Cues. Previously, MatroskaExtractor only recorded Seek entries for ID_TRACKS and ID_CUES, ignoring ID_SEEK_HEAD entries. As a result, files where Cues (or Tracks) are only indexed in the secondary SeekHead emitted an Unseekable SeekMap and could not be sought. This change: - Records the first in-bounds ID_SEEK_HEAD Seek entry position within the Segment (ignoring out-of-range offsets and unknown Segment sizes). - On encountering the first Cluster, seeks once to the secondary SeekHead if Tracks or Cues are still needed (respecting FLAG_DISABLE_SEEK_FOR_CUES). - After parsing the secondary SeekHead (or recovering if the pointer lands on a non-SeekHead element), proceeds to seek for Tracks/Cues or emits an Unseekable SeekMap, sends track formats once, and returns to the Cluster. - Resets pending SeekHead discovery state in seek() when a SeekMap has not yet been sent. Testing: - Added sample_with_chained_seek_head.mkv (derived from sample.mkv by moving SeekHead entries for Info, Tracks, Cues, and Tags into a secondary SeekHead at the end of the Segment referenced by the primary SeekHead) and parameterized ExtractorAsserts coverage in MatroskaExtractorTest reusing extractordumps/mkv/sample.mkv. - Added non-parameterized tests in MatroskaExtractorNonParameterizedTest covering FLAG_DISABLE_SEEK_FOR_CUES, a malformed SeekHead pointer targeting a Cluster, a pointer past the Segment end, and a secondary SeekHead without a Cues entry.
|
@sahuadarsh0 Thanks for the feedback, both of the edge cases should now be fixed. |
|
See #3588 (comment) |
RFC 9559 Section 6.3 states that a Matroska Segment may contain a primary SeekHead near the beginning of the file that references a single secondary SeekHead (typically written near the end of the file by older versions of mkvmerge) containing the positions of Tracks and/or Cues.
Previously,
MatroskaExtractoronly recorded Seek entries forID_TRACKSandID_CUES, ignoringID_SEEK_HEADentries. As a result, files where Cues (or Tracks) are only indexed in the secondary SeekHead emitted an Unseekable SeekMap and could not be sought.This change:
ID_SEEK_HEADSeek entry position within the Segment (ignoring out-of-range offsets and unknown Segment sizes).FLAG_DISABLE_SEEK_FOR_CUES).Testing:
sample_with_chained_seek_head.mkv(derived fromsample.mkvby moving SeekHead entries for Info, Tracks, Cues, and Tags into a secondary SeekHead at the end of the Segment referenced by the primary SeekHead) and parameterized ExtractorAsserts coverage inMatroskaExtractorTestreusing extractordumps/mkv/sample.mkv.MatroskaExtractorNonParameterizedTestcoveringFLAG_DISABLE_SEEK_FOR_CUES, a malformed SeekHead pointer targeting a Cluster, a pointer past the Segment end, and a secondary SeekHead without a Cues entry.